
import { setToken, getToken,romeveToken } from ".././utlis/cookie"

export const state = () => {
  return {
    roles: ["admin", "chen"],
    userInfo: {}
  }
}

export const mutations = {

  SET_ROLES(state, res) {
    console.log(res);
    state.roles = res

  },
  SET_USER_INFO(state, data) {
    state.userInfo = data

  }
}

export const actions = {
  async doLogout({commit}) {
    //用户退出
    commit("SET_ROLES",[])
    commit("SET_USER_INFO",{})
    romeveToken()

  },

  async getUserInfo({ commit }, token) {  //根据用户token 获取用户信息
    console.log(token);
    let r = await this.$axios.get(`http://localhost:2345/user/info?token=${token}`)

    commit("SET_ROLES", r.data.data.roles)
    commit("SET_USER_INFO", r.data.data)

  },
  async doLogin({ }, form) {

    // 根据用户名密码获取token值
    let r = await this.$axios({
      url: "http://localhost:2345/user/login",
      method: "post",
      data: {
        loginName: form.loginName,
        loginKey: form.loginKey
      }
    })

    // 根据token 获取用户信息
    setToken(r.data.data.token)  //将token储存到本地

    // console.log(getToken());


  }


}

export const getters = {

}